﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace TPA.DataAccess
{
    public interface ITimeLineBLL
    {
        void Add(TimelineSegment td);
        void Remove(TimelineSegment td);
        TimelineSegment Get(string channel, Guid programId, DateTime startdatetime);
        List<TimelineSegment> GetList();
        bool IsExist(TimelineSegment td);
        IEnumerable<vwTimelineSegment> GetTimelineSegment(DateTime date, string channel);
        IEnumerable<vwTimelineSegment> GetTimelineSegment(string date);
        IEnumerable<vwTimelineSegment> GetTimelineSegmentByChannel(string channel);
        void SaveChanges();
        void Refresh();
        void RefreshView();
        /// <summary>
        /// Get channels in a specific date
        /// </summary>
        /// <param name="date">date</param>
        /// <returns></returns>
        string[] GetChannel(string date);
        /// <summary>
        /// get all channels in timeline
        /// </summary>
        /// <returns></returns>
        string[] GetChannel();
        /// <summary>
        /// get all years in timeline
        /// </summary>
        /// <returns>yyyy</returns>
        string[] GetYears();
        /// <summary>
        /// get months in a specific year
        /// </summary>
        /// <param name="year">yyyy</param>
        /// <returns>mm</returns>
        string[] GetMonthByYear(string year);
        /// <summary>
        /// Get date in a specific month of a specific year
        /// </summary>
        /// <param name="year">yyyy</param>
        /// <param name="month">mm</param>
        /// <returns>mm/dd/yyyy</returns>
        string[] GetDayByYearAndMonth(string year, string month);
        /// <summary>
        /// Get years of a specific channel
        /// </summary>
        /// <param name="channelname"></param>
        /// <returns>yyyy</returns>
        string[] GetYearInChannel(string channelname);
        /// <summary>
        /// Get date of a specific channel in a specific year
        /// </summary>
        /// <param name="year"></param>
        /// <param name="channelname"></param>
        /// <returns></returns>
        string[] GetDateInYearAndChannel(string year, string channelname);
        void DeleteByChannelNameandDate(string channelname, string date);
    }

    public class TimeLineBLL:ITimeLineBLL
    {
        TVProgamAnalyticEntities myEntities = new TVProgamAnalyticEntities();

        public void Add(TimelineSegment td)
        {
            myEntities.TimelineSegments.AddObject(td);
            //throw new NotImplementedException();
        }

        public void Remove(TimelineSegment td)
        {
            TimelineSegment tdtarget = Get(td.ChannelName, td.ProgramID, td.StartDateTime);
            if(tdtarget!=null)
                myEntities.TimelineSegments.DeleteObject(tdtarget);
            //throw new NotImplementedException();
        }

        public TimelineSegment Get(string channel, Guid programId, DateTime startdatetime)
        {
            //if (myEntities.TimelineSegments.Count() > 0)
            //{
            //    return myEntities.TimelineSegments.Single(t => t.Channel == channel && t.StartDateTime == startdatetime && t.ProgramID == programId);
            //}
            var tr=from t in myEntities.TimelineSegments
                       where t.ChannelName == channel && t.StartDateTime == startdatetime && t.ProgramID == programId
                       select t;
            if (tr.Count() > 0)
                return tr.Single();
            else
                return null;
            //throw new NotImplementedException();
        }

        public List<TimelineSegment> GetList()
        {
            return myEntities.TimelineSegments.ToList();
            //throw new NotImplementedException();
        }

        public bool IsExist(TimelineSegment td)
        {
            TimelineSegment tdtarget = Get(td.ChannelName, td.ProgramID, td.StartDateTime);
            if (tdtarget != null)
                return true;
            else
                return false;

            //throw new NotImplementedException();
        }

        public IEnumerable<vwTimelineSegment> GetTimelineSegment(DateTime date, string channel)
        {
            //TVProgamAnalyticEntities entities = new TVProgamAnalyticEntities();
                DateTime d1 = date.Date;
                DateTime d2 = date.AddDays(1).Date;
                string d = date.ToString("MM/dd/yyyy");
                var result = from tf in myEntities.vwTimelineSegments
                             where tf.ChannelName == channel && (tf.CDate==d)
                             orderby tf.StartDateTime,tf.EndTime
                             select tf;
                return result;
            //throw new NotImplementedException();
        }


        public void SaveChanges()
        {
                myEntities.SaveChanges();
            //throw new NotImplementedException();
        }

        public void Refresh()
        {
            myEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, myEntities.TimelineSegments);
            //throw new NotImplementedException();
        }


        public void RefreshView()
        {
            myEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, myEntities.vwTimelineSegments);
            //throw new NotImplementedException();
        }


        public string[] GetChannel()
        {
            //var channels = (from tl in myEntities.vwTimelineSegments
            //                select tl.ChannelName).Distinct();
            //return channels.ToArray();
            return myEntities.GetAllChannels().ToArray();
            //throw new NotImplementedException();
        }

        public string[] GetChannel(string date)
        {
            return myEntities.GetChannelByDate(date).ToArray();
            //throw new NotImplementedException();
        }




        public string[] GetYears()
        {
            return myEntities.GetYears().ToArray();
            //throw new NotImplementedException();
        }

        public string[] GetMonthByYear(string year)
        {
            return myEntities.GetMonthByYear(year).ToArray();
            //throw new NotImplementedException();
        }

        public string[] GetDayByYearAndMonth(string year, string month)
        {
            return myEntities.GetDateByYearAndMonth(year, month).ToArray();
            //throw new NotImplementedException();
        }


        public IEnumerable<vwTimelineSegment> GetTimelineSegment(string date)
        {
            var result = from vts in myEntities.vwTimelineSegments
                         where vts.CDate == date
                         orderby vts.StartDateTime,vts.EndTime
                         select vts;
            return result;
            //throw new NotImplementedException();
        }


        public IEnumerable<vwTimelineSegment> GetTimelineSegmentByChannel(string channel)
        {
            var result = from vts in myEntities.vwTimelineSegments
                         where vts.ChannelName==channel
                         orderby vts.StartDateTime,vts.EndTime
                         select vts;
            return result;
            //throw new NotImplementedException();
        }




        public string[] GetYearInChannel(string channelname)
        {
            return myEntities.GetAllYearInChannel(channelname).ToArray();
            //throw new NotImplementedException();
        }

        public string[] GetDateInYearAndChannel(string year, string channelname)
        {
            return myEntities.GetAllDateInYearAndChannel( channelname,year).ToArray();
            //throw new NotImplementedException();
        }


        public void DeleteByChannelNameandDate(string channelname, string date)
        {
            myEntities.DeleteByChannelandDate(channelname, date);
        }
    }
}
